package com.future.lock.common.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.EventBusUtil;
import com.future.lock.R;
import com.future.lock.common.Constants;
import com.future.lock.common.MyApp;
import com.future.lock.find.FindFragment;
import com.future.lock.home.HomeFragment;
import com.future.lock.home.activity.LockNotesActivity;
import com.future.lock.mall.MallFragment;
import com.future.lock.me.PersonFragment;
import com.future.lock.me.activity.LoginActivity;
import com.jaeger.library.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @BindView(R.id.rg_bottom)
    RadioGroup rgBottom;
    @BindView(R.id.rb_tab_1)
    RadioButton rbTab1;
    @BindView(R.id.rb_tab_2)
    RadioButton rbTab2;
    @BindView(R.id.rb_tab_3)
    RadioButton rbTab3;
    @BindView(R.id.rb_tab_4)
    RadioButton rbTab4;
    @BindView(R.id.view_line)
    View viewLine;

    private String[] tags = {"tag1", "tag2", "tag3", "tag4"};

    int currentPosition = 0;
    private Badge badge;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        rgBottom.setOnCheckedChangeListener(this);
        rbTab1.setChecked(true);
        ivTitleLeft.setVisibility(View.GONE);
        setTitleRight(R.mipmap.ic_title_right, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(MyApp.getUserToken())) {
                    startActivity(LoginActivity.class);
                }else {
                    startActivity(LockNotesActivity.class);
                    EventBusUtil.sendEvent(new MessageEvent(Constants.RED_POINT_NO));
                }
            }
        });

        badge = new QBadgeView(this)
                .bindTarget(ivTitleRight)
                .setBadgeGravity(Gravity.END | Gravity.TOP)
                .setGravityOffset(16, 9, true)
                .setBadgeNumber(0);
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(MessageEvent event) {
        if (event.getCode() == Constants.RED_POINT) {
            badge.setBadgeNumber(-1);
        }else if (event.getCode() == Constants.RED_POINT_NO){
            badge.setBadgeNumber(0);
        }
    }

    @Override
    protected void initLogic() {
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        boolean logout = intent.getBooleanExtra("logout", false);
        if (logout) {
            rbTab1.setChecked(true);
        }
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragment1 = fragmentManager.findFragmentByTag(tags[0]);
        Fragment fragment2 = fragmentManager.findFragmentByTag(tags[1]);
        Fragment fragment3 = fragmentManager.findFragmentByTag(tags[2]);
        Fragment fragment4 = fragmentManager.findFragmentByTag(tags[3]);

        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
        if (fragment4 != null) {
            transaction.hide(fragment4);
        }

        setUpTitle(checkedId);
        switch (checkedId) {
            case R.id.rb_tab_1:
                if (fragment1 == null) {
                    fragment1 = new HomeFragment();
                    transaction.add(R.id.fragment_container, fragment1, tags[0]);
                } else {
                    transaction.show(fragment1);
                }
                currentPosition = 0;
                break;
            case R.id.rb_tab_2:
                if (fragment2 == null) {
                    fragment2 = new MallFragment();
                    transaction.add(R.id.fragment_container, fragment2, tags[1]);
                } else {
                    transaction.show(fragment2);
                }
                currentPosition = 1;
                break;
            case R.id.rb_tab_3:
                if (fragment3 == null) {
                    fragment3 = new FindFragment();
                    transaction.add(R.id.fragment_container, fragment3, tags[2]);
                } else {
                    transaction.show(fragment3);
                }
                currentPosition = 2;
                break;
            case R.id.rb_tab_4:
                if (!MyApp.getInstance().hasLogin()) {
                    startActivity(LoginActivity.class);
                    ((RadioButton) rgBottom.getChildAt(currentPosition)).setChecked(true);
                    return;
                }
                if (fragment4 == null) {
                    fragment4 = new PersonFragment();
                    transaction.add(R.id.fragment_container, fragment4, tags[3]);
                } else {
                    ((PersonFragment) fragment4).initView();
                    transaction.show(fragment4);
                }
                currentPosition = 3;
                break;
        }
        transaction.commitNow();
    }

    private void setUpTitle(int checkedId) {
        switch (checkedId) {
            case R.id.rb_tab_1:
                StatusBarUtil.setColor(this, Color.WHITE);
                titleBar.setVisibility(View.GONE);
                break;
            case R.id.rb_tab_2:
                StatusBarUtil.setColor(this, Color.WHITE);
                titleBar.setVisibility(View.VISIBLE);
                titleBar.setBackgroundColor(Color.WHITE);
                ivTitleRight.setImageResource(R.mipmap.ic_title_right);
                viewLine.setVisibility(View.VISIBLE);
                setTitle("商城");
                break;
            case R.id.rb_tab_3:
                StatusBarUtil.setColor(this, Color.WHITE);
                titleBar.setVisibility(View.VISIBLE);
                titleBar.setBackgroundColor(Color.WHITE);
                ivTitleRight.setImageResource(R.mipmap.ic_title_right);
                viewLine.setVisibility(View.VISIBLE);
                setTitle("发现");
                break;
            case R.id.rb_tab_4:
                StatusBarUtil.setColor(this, Color.parseColor("#648ddd"));
                titleBar.setVisibility(View.VISIBLE);
                setTitle("");
                titleBar.setBackgroundColor(Color.parseColor("#648ddd"));
                ivTitleRight.setImageResource(R.mipmap.ic_title_right_white);
                viewLine.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        for (int i = 0; i < rgBottom.getChildCount(); i++) {
            RadioButton mTab = (RadioButton) rgBottom.getChildAt(i);
            FragmentManager manager = getSupportFragmentManager();
            Fragment fragment = manager.findFragmentByTag(tags[i]);
            FragmentTransaction transaction = manager.beginTransaction();

            if (fragment != null) {
                if (!mTab.isChecked()) {
                    transaction.hide(fragment);
                }
            }
            transaction.commit();
        }
    }
}
